package com.tb.shoop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.tb.v1.api.shoop.IGoodsTransactionService;
import com.tb.v1.api.shoop.ISpGoodsService;
import com.tb.v1.api.vo.SpGoodsVo;
import com.tb.v1.common.exception.CommonException;
import com.tb.v1.entity.SpGoods;
import com.tb.v1.result.ResultJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: tangBin
 * @Date: 2020/7/22 0022 12:57
 * @Description:
 */
@RequestMapping("/v1")
@RestController
public class SpGoodsController extends BaseController {
    @Reference
    private ISpGoodsService spGoodsService;
    @Reference
    private IGoodsTransactionService goodsTransactionService;

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 商品列表
     * @Date 2020/7/22 0022 13:02
     * @Param []
     **/
    @RequestMapping("goods")
    public ResultJson<Object> getGoodsList(SpGoodsVo vo) {
        Map map = new HashMap();
        Integer page = (vo.getPageIndex() - 1) * vo.getPageSize();
        map.put("page", page);
        map.put("size", vo.getPageSize());
        map.put("goodsName", vo.getQuery());
        List<SpGoods> goods = spGoodsService.getGoodList(map);
        Integer totalNum = 0;
        if (!goods.isEmpty()) {
            totalNum = spGoodsService.getGoodListCount(map);
        }
        Map resultMap = new HashMap();
        resultMap.put("total", totalNum);
        resultMap.put("pageIndex", vo.getPageIndex());
        resultMap.put("goods", goods);
        return toResultModel("200", "成功", resultMap);
    }

    @RequestMapping("delete/goods")
    public ResultJson<Object> getDeleteGoods(String goodsId) throws Exception {
        if (StringUtils.isEmpty(goodsId)) {
            throw new CommonException("500", "参数为空");
        }
        Integer num = spGoodsService.deleteEntity(Long.valueOf(goodsId));
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("500", "失败");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 新增商品信息
     * @Date 2020/7/23 0023 12:05
     * @Param [goodsId]
     **/
    @RequestMapping("add/goods")
    public ResultJson<Object> getAddGoods(@RequestBody SpGoodsVo vo) throws Exception {
        if (StringUtils.isEmpty(vo.getGoodsName()) ||
                StringUtils.isEmpty(vo.getGoodsPrice()) ||
                StringUtils.isEmpty(vo.getGoodsNumber()) ||
                StringUtils.isEmpty(vo.getGoodsWeight()) ||
                StringUtils.isEmpty(vo.getGoodsCat())) {
            throw new CommonException("500", "参数为空");
        }
        Integer num = goodsTransactionService.addGoods(vo);
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("500", "失败");
        }
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 图片上传
     * @Date 2020/7/24 0024 12:10
     * @Param []
     **/
    @RequestMapping("upload")
    public ResultJson<Object> getUpload() throws Exception {
        String url = "https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100";
        return toResultModel("500", "失败", url);
    }

    /**
     * @return com.tb.v1.result.ResultJson<java.lang.Object>
     * @Author tangBin
     * @Description 新增商品信息
     * @Date 2020/7/23 0023 12:05
     * @Param [goodsId]
     **/
    @RequestMapping("update/goods")
    public ResultJson<Object> getUpdateGoods(@RequestBody SpGoodsVo vo) throws Exception {
        if (StringUtils.isEmpty(vo.getGoodsName()) ||
                StringUtils.isEmpty(vo.getGoodsPrice()) ||
                StringUtils.isEmpty(vo.getGoodsNumber()) ||
                StringUtils.isEmpty(vo.getGoodsWeight()) ||
                StringUtils.isEmpty(vo.getGoodsCat())) {
            throw new CommonException("500", "参数为空");
        }
        Integer num = goodsTransactionService.updateGoods(vo);
        if (num > 0) {
            return toResultModel("200", "成功");
        } else {
            return toResultModel("500", "失败");
        }
    }
}
